--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 消息的处理

local skynet = require "skynet"
local ServerInfo = require("ServerInfo")

local table_insert = table.insert
local table_remove = table.remove

local MessageHandler = class("MessageHandler")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function MessageHandler:ctor(message_dispatch)
	
    self.message_dispatch = message_dispatch
    self:register()

end

--注册本服务里的消息
function MessageHandler:register()

	self.message_dispatch:registerSelf('start', handler(self, self.start))
	self.message_dispatch:registerSelf('server_register_req', handler(self, self.registerReq))
    
end

---------------------------------------------------------
-- cmd
---------------------------------------------------------

--启动
function MessageHandler:start()

    -- self.server_manager = global.server_manager    
    self.node_message = global.node_message
    self.server_info = global.server_info
    -- self.user_manager = global.user_manager
    self.cluster_manager = global.cluster_manager

    self.redis = skynet.newservice("redis_service")
    local server_list = skynet.call(self.redis, "lua", "normal_cmd", "keys", "cluster:*")
    for _, v in pairs(server_list) do 
        skynet.call(self.redis, "lua", "normal_cmd", "del", v)
    end

    --集群心跳检查
    skynet.fork(function()
        while true do 
            skynet.sleep(500)
            self:checkServer()
        end
    end)
    
end

--收到别的服务器注册
function MessageHandler:registerReq(data)
    debug_log("_________收到服务器注册______",data)
    --注册信息有问题
    if not data or not data.id then 
        error_log("__服务器注册失败__", data)
        return 
    end    
    local server_name = data.name
    local server_info = self.cluster_manager:getServer(server_name)
    if not server_info then 
        server_info = ServerInfo.new()
    end
    server_info:setData(data) 
    -- self.server_manager:addServer(server_info)
    self.cluster_manager:addServer(server_info)
    
    --添加到redis
    local server_data = server_info:getData()
    server_data = table.to_array(server_data)
    skynet.call(self.redis, "lua", "normal_cmd", "hmset", "cluster:"..server_name,  table.unpack(server_data))

    --通知各个服务器更新集群列表
    for k, v in pairs(self.cluster_manager:getClusterList()) do 
        self.node_message:callNodeManager(k, "cluster_update_nt", self.cluster_manager:getServerList())
    end
        
    return true
end

--集群心跳检查
function MessageHandler:checkServer()
    local remove_list = {} --已断线的服务器
    local server_list = self.cluster_manager:getServerList()
    for name, server_info in pairs(server_list) do 
        local ok, res = self.node_message:callNodeManager(name, "cluster_heart_req", os.time())
        -- debug_log("______checkServer____res___", res)
        if not ok or not res then 
            --服务器断开了
            self.cluster_manager:removeServer(name)
            table_insert(remove_list, server_info)
        end
        
    end
    for k, server_info in pairs(remove_list) do 
        skynet.call(self.redis, "lua", "normal_cmd", "del", "cluster:"..server_info.name)
    end
    if next(remove_list) then 
        for name, server_info in pairs(server_list) do
            local res = self.node_message:callNodeManager(name, "cluster_update_nt", self.cluster_manager:getServerList())
        end
    end
end



return MessageHandler